home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / modex32w.zip / SC2SCX.ASM < prev    next >
Assembly Source File  |  1995-02-21  |  8KB  |  173 lines

  1.         .386p
  2.         locals
  3.         include w.inc
  4.  
  5. ; Mode X (320x240, 256 colors) display memory to display memory masked copy 
  6. ; routine. Works on all VGAs. Uses approach of reading 4 pixels at a time from 
  7. ; source into latches, then writing latches to destination, using Map Mask 
  8. ; register to perform masking. Copies up to but not including column at 
  9. ; SourceEndX and row at SourceEndY. No clipping is performed. Results are not 
  10. ; guaranteed if source and destination overlap. C near-callable as:
  11. ;    void CopyScreenToScreenMaskedX(int SourceStartX,
  12. ;       int SourceStartY, int SourceEndX, int SourceEndY,
  13. ;       int DestStartX, int DestStartY, MaskedImage * Source,
  14. ;       unsigned int DestPageBase, int DestBitmapWidth);
  15.  
  16. SC_INDEX equ    03c4h   ;Sequence Controller Index register port
  17. MAP_MASK equ    02h     ;index in SC of Map Mask register
  18. GC_INDEX equ    03ceh   ;Graphics Controller Index register port
  19. BIT_MASK equ    08h     ;index in GC of Bit Mask register
  20.  
  21. parms   struc
  22.         dd      2 dup (?) ;pushed BP and return address
  23. SourceStartX dd ?       ;X coordinate of upper left corner of source
  24. SourceStartY dd ?       ;Y coordinate of upper left corner of source
  25. SourceEndX   dd ?       ;X coordinate of lower right corner of source
  26.                         ; (the column at SourceEndX is not copied)
  27. SourceEndY   dd ?       ;Y coordinate of lower right corner of source
  28.                         ; (the row at SourceEndY is not copied)
  29. DestStartX   dd ?       ;X coordinate of upper left corner of dest
  30. DestStartY   dd ?       ;Y coordinate of upper left corner of dest
  31. Source       dd ?       ;pointer to MaskedImage struct for source
  32.                         ; which source resides
  33. DestPageBase dd ?       ;base offset in display memory of page in
  34.                         ; which dest resides
  35. DestBitmapWidth   dd ?  ;# of pixels across dest bitmap (must be multiple of 4)
  36. parms   ends
  37.  
  38. SourceNextScanOffset equ -4   ;local storage for distance from end of
  39.                               ; one source scan line to start of next
  40. DestNextScanOffset equ -8    ;local storage for distance from end of
  41.                               ; one dest scan line to start of next
  42. RectAddrWidth equ -12   ;local storage for address width of rectangle
  43. RectHeight    equ -16   ;local storage for height of rectangle
  44. SourceBitmapWidth equ -20 ;local storage for width of source bitmap
  45.                         ; (in addresses)
  46. STACK_FRAME_SIZE equ 20
  47.  
  48. MaskedImage     struc
  49.  Alignments     dd  4 dup(?) ;pointers to AlignedMaskedImages for the
  50.                              ; 4 possible destination image alignments
  51. MaskedImage     ends
  52.  
  53. AlignedMaskedImage      struc
  54.  ImageWidth     dd      ? ;image width in addresses (also mask width in bytes)
  55.  ImagePtr       dd      ? ;offset of image bitmap in display memory
  56.  MaskPtr        dd      ? ;pointer to mask bitmap in DS
  57. AlignedMaskedImage      ends
  58.  
  59.         @dseg
  60.  
  61.         extrn   SCREEN_SEG:dword
  62.  
  63.         ends
  64.  
  65.         @cseg
  66.  
  67.         public  _CopyScreenToScreenMaskedX
  68. _CopyScreenToScreenMaskedX proc    near
  69.         push    ebp      ;preserve caller's stack frame
  70.         mov     ebp,esp  ;point to local stack frame
  71.         sub     esp,STACK_FRAME_SIZE ;allocate space for local vars
  72.         push    esi      ;preserve caller's register variables
  73.         push    edi
  74.         push    ebx
  75.  
  76.         cld
  77.         mov     dx,GC_INDEX     ;set the bit mask to select all bits
  78.         mov     ax,00000h+BIT_MASK ; from the latches and none from
  79.         out     dx,ax           ; the CPU, so that we can write the
  80.                                 ; latch contents directly to memory
  81.         mov     eax, [ebp+DestBitmapWidth]
  82.         shr     eax,2            ;convert to width in addresses
  83.         mul     [ebp+DestStartY] ;top dest rect scan line
  84.         mov     edi, [ebp+DestStartX]
  85.         mov     esi,edi
  86.         shr     edi,2    ;X/4 = offset of first dest rect pixel in scan line
  87.         add     edi,eax   ;offset of first dest rect pixel in page
  88.         add     edi,[ebp+DestPageBase] ;offset of first dest rect pixel
  89.                         ; in display memory. now look up the image that's 
  90.                         ; aligned to match left-edge alignment of destination
  91.         and     esi,3   ;DestStartX modulo 4
  92.         mov     ecx,esi ;set aside alignment for later
  93.         shl     esi,2    ;prepare for dword look-up
  94.         mov     ebx,[ebp+Source] ;point to source MaskedImage structure
  95.         mov     ebx,[ebx+Alignments+esi] ;point to AlignedMaskedImage
  96.                         ; struc for current left edge alignment
  97.         mov     eax,[ebx+ImageWidth] ;image width in addresses
  98.         mov     [ebp+SourceBitmapWidth],eax ;remember image width in
  99.                                           ; addresses
  100.         mul     [ebp+SourceStartY] ;top source rect scan line
  101.         mov     esi,[ebp+SourceStartX]
  102.         shr     esi,2    ;X/4 = address of first source rect pixel in scan line
  103.         add     esi,eax   ;offset of first source rect pixel in image
  104.         mov     eax,esi
  105.         add     esi,[ebx+MaskPtr] ;point to mask offset of first mask pixel in DS
  106.         mov     ebx,[ebx+ImagePtr] ;offset of first source rect pixel
  107.         add     ebx,eax            ; in display memory
  108.  
  109.         mov     eax,[ebp+SourceStartX] ;calculate # of addresses across
  110.         add     eax,ecx                ; rect, shifting if necessary to
  111.         add     ecx,[ebp+SourceEndX]   ; account for alignment
  112.         cmp     ecx,eax
  113.         jle     CopyDone        ;skip if 0 or negative width
  114.         add     ecx,3
  115.         and     eax,not 011b
  116.         sub     ecx,eax
  117.         shr     ecx,2    ;# of addresses across rectangle to copy
  118.         mov     eax,[ebp+SourceEndY]
  119.         sub     eax,[ebp+SourceStartY]  ;EAX = height of rectangle
  120.         jle     CopyDone        ;skip if 0 or negative height
  121.         mov     [ebp+RectHeight],eax
  122.         mov     eax,[ebp+DestBitmapWidth]
  123.         shr     eax,2            ;convert to width in addresses
  124.         sub     eax,ecx ;distance from end of one dest scan line to start of next
  125.         mov     [ebp+DestNextScanOffset],eax
  126.         mov     eax,[ebp+SourceBitmapWidth] ;width in addresses
  127.         sub     eax,ecx ;distance from end of source scan line to start of next
  128.         mov     [ebp+SourceNextScanOffset],eax
  129.         mov     [ebp+RectAddrWidth],ecx ;remember width in addresses
  130.  
  131.         mov     dx,SC_INDEX
  132.         mov     al,MAP_MASK
  133.         out     dx,al           ;point SC Index register to Map Mask
  134.         inc     dx              ;point to SC Data register
  135.  
  136.         mov     ecx, [SCREEN_SEG]
  137.         add     edi, ecx        ;edi & ebx point to video memory
  138.         add     ebx, ecx
  139. CopyRowsLoop:
  140.         mov     ecx,[ebp+RectAddrWidth] ;width across
  141. CopyScanLineLoop:
  142.         lodsb                   ;get the mask for this four-pixel set
  143.                                 ; and advance the mask pointer
  144.         out     dx,al           ;set the mask
  145.         mov     al,[ebx]        ;load the latches with 4-pixel set from source
  146.         mov     [edi],al        ;copy the four-pixel set to the dest
  147.         inc     ebx             ;advance the source pointer
  148.         inc     edi             ;advance the destination pointer
  149.         dec     ecx             ;count off four-pixel sets
  150.         jnz     CopyScanLineLoop
  151.  
  152.         mov     eax,[ebp+SourceNextScanOffset]
  153.         add     esi,eax                      ;point to the start of
  154.         add     ebx,eax                      ; the next source, mask,
  155.         add     edi,[ebp+DestNextScanOffset] ; and dest lines
  156.         dec     dword ptr [ebp+RectHeight] ;count down scan lines
  157.         jnz     CopyRowsLoop
  158. CopyDone:
  159.         mov     dx,GC_INDEX+1   ;restore the bit mask to its default,
  160.         mov     al,0ffh         ; which selects all bits from the CPU
  161.         out     dx,al           ; and none from the latches (the GC
  162.                                 ; Index still points to Bit Mask)
  163.         pop     ebx
  164.         pop     edi      ;restore caller's register variables
  165.         pop     esi
  166.         mov     esp,ebp  ;discard storage for local variables
  167.         pop     ebp      ;restore caller's stack frame
  168.         ret
  169. _CopyScreenToScreenMaskedX endp
  170.         ends
  171.         end
  172.  
  173.